﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XZen.Blogger;
using System.Diagnostics;
using XZen.Blogger.Execptions;
using System.Configuration;
using System.Collections;
using Cdn.Blogger.Models;
using System.Web.Routing;
using Cdn.Blogger.Controllers;
using System.IO;
using WebApi.Models;

namespace Cdn.Blogger.Controllers
{
    [Initialization]
    public class BlogUIController : UIController
    {
       
        protected string logOnUrl;
        
        public ActionResult Logon()
        {
            return RedirectToAction("Login", "Home", new { });
        }

        

        public ActionResult Index()
        {
            //if (IsServiceNotInitiated) return Logon();
            List<Blog> blogs = Service.GetBlogs();
            return View(blogs);
        }      
       
        //
        // GET: /BlogUI/Details/5

        public ActionResult Details(string id = null)
        {
            
            long? blogId = id.ToNullableLong();
            var blog = Service.GetBlog(blogId);
        
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Create

        public ActionResult Create()
        {
            
            return View();
        }

        //
        // POST: /BlogUI/Create

        [HttpPost]
        public ActionResult Create(Blog blog)
        {
            
            try
            {
                if (ModelState.IsValid)
                {
                    Service.CreateBlog(blog);
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Edit/5

        public ActionResult Edit(string id = null)
        {
            
            long? blogId = id.ToNullableLong();
            var blog = Service.GetBlog(blogId);
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // POST: /BlogUI/Edit/5

        [HttpPost]
        public ActionResult Edit(Blog blog)
        {
            
            long? blogId = blog.ToID();
            if (ModelState.IsValid)
            {
                var blogServer = Service.GetBlog(blogId);
                if (blogServer == null) return View(blog);
                Service.EditBlog(blog);
                return RedirectToAction("Index");
            }
            return View(blog);
        }

        //
        // GET: /BlogUI/Delete/5

        public ActionResult Delete(string id = null)
        {
            
            long? blogId = id.ToNullableLong();
            var blog = Service.GetBlog(blogId);
            if (blog == null)
            {
                return HttpNotFound();
            }
            return View(blog);
        }

        //
        // POST: /BlogUI/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            
            long? blogId = id.ToNullableLong();
            var blog = Service.GetBlog(blogId);
            Service.DeleteBlog(blog);
            return RedirectToAction("Index");
        }
        public ActionResult Posting(string id)
        {
            
            long? blogId = id.ToNullableLong();
            Blog blog = Service.GetBlog(blogId);
            List<Posting> posts = Service.GetPostings(blogId);
            ViewBag.Blog = blog;
            ViewBag.BlogID = id;
            return View(posts);
        }
        /// <summary>
        /// ////////////////////////////////////////////////////////
        /// </summary>
        /// <param name="id"></param>
        /// <param name="blogId"></param>
        /// <returns></returns>
        public ActionResult CreatePosting(string id, string blogId)
        {
            
            Posting post = new Posting();
            post.BlogID = blogId;
            ViewBag.BlogID = id;
            return View(post);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreatePosting(Posting posting)
        {
            
            ViewBag.BlogID = posting.BlogID;
            try
            {
                long? blogId = posting.ToBlogID();
                if (ModelState.IsValid)
                {
                    Service.CreatePosting(blogId, posting);
                    return RedirectToAction("Posting", new { id = posting.BlogID });
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(posting);
        }
        //
        // GET: /Attachement/Create

        public ActionResult CreateAttachment(string id)
        {
            Attachment att = new Attachment();
            att.ParentGuid = id;
            return View(att);
        }

        //
        // POST: /Attachement/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateAttachment(Attachment attachment, HttpPostedFileBase fileupload)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    var file = Request.Files[0];
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        BinaryReader b = new BinaryReader(file.InputStream);
                        byte[] binData = b.ReadBytes((int)(file.InputStream.Length));
                        attachment.BData = binData;
                        attachment.AbsolutePath = file.FileName;
                        attachment.DataType = Path.GetExtension(fileName);
                        attachment.RootPath = fileName;
                        attachment.Size = file.ContentLength;
                        if (string.IsNullOrEmpty(attachment.DisplayName))
                        {
                            attachment.DisplayName = fileName;
                        }
                    }
                    //db.Attachments.Add(attachment);
                    //db.SaveChanges();
                    return RedirectToAction("EditByGuid", "Bug", new { id = attachment.ParentGuid });
                }
                ModelState.AddModelError("HOL", "File Not found. ");

            }

            return View(attachment);
        }

        public ActionResult PreviewPosting(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            return View(posting);
        }
        public ActionResult PublishPosting(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            return View(posting);
        }
        [HttpPost, ActionName("PublishPosting")]
        public ActionResult PublishPostingConfirmed(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            Service.PublishPosting(posting);
            return RedirectToAction("Posting", new { id = posting.BlogID });
        }

        public ActionResult EditPosting(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            return View(posting);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditPosting(Posting posting)
        {
            
            ViewBag.BlogID = posting.BlogID;
            try
            {
                long? postingId = posting.ToID();
                if (ModelState.IsValid)
                {
                    Posting postingServer = Service.GetPosting(postingId);
                    //Exchange
                    if (postingServer == null)
                        return View(posting);
                    Service.EditPosting(posting);
                    return RedirectToAction("Posting", new { id = posting.BlogID });
                }
            }
            catch (Exception ecx)
            {
                ModelState.AddModelError("EX", ecx);
            }
            return View(posting);
        }

        public ActionResult DeletePosting(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            return View(posting);
        }
        [HttpPost, ActionName("DeletePosting")]
        public ActionResult DeletePostingConfirmed(string id)
        {
            
            long? postingId = id.ToNullableLong();
            Posting posting = Service.GetPosting(postingId);
            Service.DeletePosting(posting);
            return RedirectToAction("Posting", new { id = posting.BlogID });
        }       
        //
        // GET: /BlogUI/
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}